﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using LTJ.Common;
using LTJ.Entity;
using LTJ.FrameWork;
using LTJ.Config;
using LTJ.Web.UI;

namespace LTJ.WebPage.UserManage
{
    public partial class userlogin : UserPage
    {
        protected GeneralConfigInfo config = GeneralConfigs.GetConfig();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!SASRequest.IsPost())
            {
                if (SASRequest.GetString("result") == "1")
                {
                    WebMSG.ShowError("用户不存在或密码错误", "userlogin.aspx", true);
                    return;
                }

                if (SASRequest.GetString("result") == "2")
                {
                    WebMSG.ShowError("您的验证码错误", "userlogin.aspx", true);
                    return;
                }

                if (SASRequest.GetString("result") == "3")
                {
                    return;
                }

                if (SASRequest.GetString("result") == "4")
                {
                    WebMSG.ShowError("您所在的用户组无系统管理权限或帐号已被停用", "userlogin.aspx", true);
                    return;
                }

                if (SASRequest.GetString("result") == "5")
                {
                    WebMSG.ShowError("您的帐户已在别处上线！", "userlogin.aspx", true);
                    return;
                }
            }

            if (SASRequest.IsPost())
            {
                VerifyLoginInf();
            }
            else
            {
                Response.Redirect("userlogin.aspx?result=3");
            }
        }

        public void VerifyLoginInf()
        {
            if (!OnlineUsers.CheckUserVerifyCode(olid, SASRequest.GetString("vcode")))
            {
                Response.Redirect("userlogin.aspx?result=2");
                return;
            }
            UserInfo userInfo = null;
            userInfo = Users.GetUser(Users.CheckPassword(SASRequest.GetString("username"), Utils.MD5(SASRequest.GetString("password")), false));

            if (userInfo.id > 0)
            {
                if (userInfo.gid != Convert.ToInt16(ManageGroup.CorpGroup) || userInfo.status != 1)
                {
                    Response.Redirect("userlogin.aspx?result=4");
                    return;
                }
                FrameWorkUtil.WriteDYJUserCookies(userInfo);
                oluserinfo = OnlineUsers.UpdateInfo(config.Passwordkey, config.Onlinetimeout);
                olid = oluserinfo.id;
                userid = oluserinfo.uid;                
                //OnlineUsers.UpdateAction(olid, LTJ.Entity.OpAction.Login.ActionID);
                LTJ.Data.DataProvider.OnlineUsers.SetUserOnlineState(userInfo.id, 1, SASRequest.GetString("vcode"));
                FrameWorkUtil.WriteCookie("vercode", SASRequest.GetString("vcode"));
                OpLogs.CreateCompanyLoginLog(userInfo.id, userInfo.lname, userInfo.gid, userInfo.cid, SASRequest.GetIP());
                Response.Write("<script type=\"text/javascript\">top.location.href='index.aspx';</script>");
                Response.End();
            }
            else
            {
                Response.Redirect("userlogin.aspx?result=1");
            }
        }
    }
}